package com.ezreal.ezchat.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.ezreal.ezchat.R;
import com.ezreal.ezchat.utils.permission.PermissionManager;
import com.ezreal.ezchat.widget.MsgPwdSettingDialog;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;

public class SettingActivity extends BaseActivity
        implements MsgPwdSettingDialog.NoticeDialogListener {

    @BindView(R.id.switch_pwd)
    SwitchCompat switchPwd;
    @BindView(R.id.layout_clear_chat_msg)
    LinearLayout layoutClearChatMsg;
    @BindView(R.id.switch_secret_message)
    SwitchCompat switchSecretMsg;

    private AlertDialog alertDialog;
    private boolean isRealChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarColor(R.color.app_blue_color);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setTitleBar(getResources().getString(R.string.setting), true, false);
        switchPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isRealChecked) {
                    isRealChecked = true;
                    return;
                }
                MsgPwdSettingDialog dialog = MsgPwdSettingDialog.newInstance(
                        isChecked ? MsgPwdSettingDialog.MSG_PWD_OPEN
                                : MsgPwdSettingDialog.MSG_PWD_CLOSE);
                dialog.show(getSupportFragmentManager(), "msgPwd");
            }
        });
        switchSecretMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                PermissionManager.getInstance().setSecretMsg(isChecked);
            }
        });
        layoutClearChatMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog = new AlertDialog.Builder(SettingActivity.this).setTitle("清空聊天记录")
                        .setMessage("确定要清空所有的聊天记录吗？")
                        .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NIMClient.getService(MsgService.class).clearMsgDatabase(true);
                            }
                        })
                        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        switchSecretMsg.setChecked(PermissionManager.getInstance().getSecretMsg());
        switchPwd.setChecked(PermissionManager.getInstance().isPermissionOpen());
        isRealChecked = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }

    @OnClick(R.id.iv_back_btn)
    public void finishActivity() {
        this.finish();
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        dialog.dismiss();
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        dialog.dismiss();
        isRealChecked = false;
        switchPwd.setChecked(!switchPwd.isChecked());
    }
}
